<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script type="text/javascript"
            src="../lib/vue/jquery.min.js"></script>
    <script type="text/javascript" src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <script type="text/javascript" src="../js/global.js"></script>
    <script type="text/javascript" src="../js/store.js"></script>
    <script type="text/javascript" src="../js/cookies.js"></script>
    <script src="../lib/vue/vue.min.js"></script>
    <script src="../lib/vue/axios.min.js"></script>
</head>

<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-form" lay-filter="">
                        <div class="layui-form-item">
                            <input id="L_userId" name="id" type="hidden">
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">当前密码</label>
                            <div class="layui-input-inline">
                                <input type="password" name="oldPass" lay-verify="oldpass" lay-verType="tips"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">新密码</label>
                            <div class="layui-input-inline">
                                <input id="newpass" type="password" name="newPass" lay-verify="newpass"
                                       lay-verType="tips"
                                       autocomplete="off" id="LAY_password" class="layui-input">
                            </div>
                            <div class="layui-form-mid layui-word-aux">长度不能小于六位</div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">确认新密码</label>
                            <div class="layui-input-inline">
                                <input type="password" name="repassword" lay-verify="repass" lay-verType="tips"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="setmypass">确认修改</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'layer'], function () {
        var form = layui.form
        $('#L_userId').val(getCookie("userId"))
        //自定义验证规则
        form.verify({
            oldpass: function (value, item) {
                if (value.length == 0) {
                    return '请输入当前密码';
                }
                var code = 0;
                $.ajax({
                    url: _hostUrl + "/login", //数据接口
                    type: "post",
                    async: false,
                    data: {
                        username: getCookie('username'),
                        password: value
                    },
                    dataType: "html",
                    success: function (res) {
                        //转换相应数据格式json串--obj
                        let resObj = JSON.parse(res);
                        code = resObj.responseCode;
                    }
                });
                if (code != 200) {
                    return "当前密码输入错误，请重新输入！";
                }
            },
            newpass: function (value, item) {
                if (value.length < 6) {
                    return "密码长度不能小于6位";
                }
            },
            repass: function (value, item) {
                if (!new RegExp($("#newpass").val()).test(value)) {
                    return "两次输入密码不一致，请重新输入！";
                }
            }
        });
        //监听提交
        form.on('submit(setmypass)', function (data) {
            $.putForm(_hostUrl + '/setPass', data.field, function (res) {
                if (res.responseCode == 201) {
                    layer.msg("密码修改成功,请重新登陆", {icon: 1, time: 2000}, function () {
                        parent.location.href = '../login.html'
                    });
                    return
                } else {
                    layer.msg(res.responseMsg, {icon: 2, time: 2000});
                }
            }, 'json');
            return false;
        });

    })
</script>
</body>
</html>